import path from "path";
import { fileURLToPath, URL } from "node:url";

import { defineConfig } from "vite";
// import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import Icons from "unplugin-icons/vite";
import IconsResolver from "unplugin-icons/resolver";
import vue from "@vitejs/plugin-vue";

// const pathSrc = path.resolve(__dirname, "src");

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    // AutoImport({
    //   // 自动导入 Vue 相关函数，如：ref, reactive, toRef 等
    //   imports: ['vue', 'vue-router'],
    //   resolvers: [
    //     // 自动导入 Element Plus 相关函数，如：ElMessage, ElMessageBox... (带样式)
    //     ElementPlusResolver(),
    //     // 自动导入图标组件
    //     IconsResolver({
    //       prefix: "Icon",
    //     }),
    //   ],
    //   eslintrc: {
    //     enabled: false, // Default `false`
    //     // filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
    //     globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
    //   },
    //   dts: true, // auto-imports.d.ts 生成位置
    // }),
    Components({
      resolvers: [
        // 自动注册图标组件
        IconsResolver({
          enabledCollections: ["ep"],
        }),
        // 自动导入 Element Plus 组件
        ElementPlusResolver({
          importStyle: "sass",
        }),
      ],
      // types: [{ // 默认值
      //   from: 'vue-router',
      //   names: ['RouterLink', 'RouterView'],
      // }],
      dts: true, // 自动生成 components.d.ts 也可以接受一个路径
      // dts: path.resolve(pathSrc, "components.d.ts"),
    }),
    Icons({
      autoInstall: true,
    }),
  ],
  base: "/pc/",
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
  css: {
    preprocessorOptions: {
      sass: {
        additionalData: `@use "@/assets/element/index.scss" as *;`,
      },
    },
  },
  build: {
    // 生产环境自动删除 console
    terserOptions: {
      compress: {
        drop_console: true, // 自动去除 console
        drop_debugger: true, // 清除 debugger 语句
      },
      ie8: true,
      output: { comments: true }, // 删除注释
    },
  },
});
